package ssm.blog.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import ssm.blog.entity.Blogger;
import ssm.blog.entity.PageBean;
import ssm.blog.entity.Picture;
import ssm.blog.service.BloggerService;
import ssm.blog.service.PictureService;
import ssm.blog.util.CryptographyUtil;
import ssm.blog.util.PageUtil;
import ssm.blog.util.StringUtil;

/**
 * @Description 博主Controller层，前台部分，不需要认证
 * @author ZeusML
 *
 */
@Controller
@RequestMapping("/blogger")
public class BloggerController {

	@Resource
	private BloggerService bloggerService;
	@Resource
	private PictureService pictureService;

	@RequestMapping("/login")
	public String login(Blogger blogger, HttpServletRequest request) {

		Subject subject = SecurityUtils.getSubject(); // 获取当前登陆的主体
		String newPassword = CryptographyUtil.md5(blogger.getPassword(), "javacoder");// 将密码使用md5加密
		// 将用户信息封装到token中
		UsernamePasswordToken token = new UsernamePasswordToken(blogger.getUsername(), newPassword);
		try {
			subject.login(token); // 会调用MyRealm中的doGetAuthenticationInfo方法进行身份认证
			return "redirect:/admin/main.jsp";
		} catch (AuthenticationException e) {
			e.printStackTrace();
			request.setAttribute("bloger", blogger);
			request.setAttribute("errorInfo", "用户名或密码错误");
			return "login";
		}

	}

	@RequestMapping("/aboutme")
	public ModelAndView abouotMe() {
		ModelAndView modelAndView = new ModelAndView();
		Blogger blogger = bloggerService.getBloggerData();
		modelAndView.addObject("blogger", blogger);
		modelAndView.addObject("commonPage", "foreground/blogger/bloggerInfo.jsp");
		modelAndView.addObject("title", "关于博主 - ZL的博客");
		modelAndView.setViewName("mainTemp");
		return modelAndView;
	}

	@RequestMapping("/myalbum")
	public ModelAndView myAlbum(@RequestParam(value = "page", required = false) String page,
			HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		if (StringUtil.isEmpty(page)) {
			page = "1";
		}
		// 获取分页的bean
		PageBean pageBean = new PageBean(Integer.parseInt(page), 16); // 每页显示16条数据
		// map中封装起始页和每页的记录
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", pageBean.getStart());
		map.put("pageSize", pageBean.getPageSize());
		List<Picture> pictureList = this.pictureService.listPicture(map);
		modelAndView.addObject("pictureList", pictureList);

		// 分页
		modelAndView.addObject("pageCode",
				PageUtil.genPagination( // 调用代码生成的工具类生成前台显示
						request.getContextPath() + "/blogger/myalbum.html", // 还是请求该controller的index方法
						pictureService.getTotal(), Integer.parseInt(page), 16, ""));
		modelAndView.addObject("commonPage", "foreground/blogger/myAlbum.jsp");
		modelAndView.setViewName("mainTemp");
		return modelAndView;
	}

	@RequestMapping("/resource")
	public ModelAndView resource() {
		ModelAndView modelAndView = new ModelAndView();
		//
		// ....
		modelAndView.addObject("commonPage", "foreground/blogger/resource.jsp");
		modelAndView.setViewName("mainTemp");
		return modelAndView;
	}
}
